package com.lc.soft.service.impl;

import com.lc.soft.dao.IDistrictDao;
import com.lc.soft.entity.CommonParameter;
import com.lc.soft.entity.DistrictEntity;
import com.lc.soft.entity.DistrictNestingEntity;
import com.lc.soft.service.IDistrictService;
import com.lc.soft.utils.CommonUtils;
import com.lc.soft.utils.ResponseResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Log4j2
public class DistrictService implements IDistrictService {

    private IDistrictDao districtDao;

    @Autowired
    public DistrictService(IDistrictDao districtDao) {
        this.districtDao = districtDao;
    }

    @Override
    public ResponseResult create(DistrictEntity districtEntity) {
        try {
            int createResult = districtDao.create(districtEntity);
            if (createResult == 0) {
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult update(DistrictEntity districtEntity) {
        try {
            int updateResult = districtDao.update(districtEntity);
            if (updateResult == 0) {
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult enable(DistrictEntity districtEntity) {
        try {
            int updateResult = districtDao.enable(districtEntity);
            if (updateResult == 0) {
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult disable(DistrictEntity districtEntity) {
        try {
            int updateResult = districtDao.disable(districtEntity);
            if (updateResult == 0) {
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult delete(DistrictEntity districtEntity) {
        try {
            int deleteResult = districtDao.delete(districtEntity);
            if (deleteResult == 0) {
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult findDistrictListByCondition(CommonParameter commonParameter) {
        try {
            List<DistrictEntity> list = districtDao.findDistrictListByCondition(commonParameter);
            if (list == null || list.size() == 0) {
                return ResponseResult.DATA_NOT_AVAILABLE;
            }
            return ResponseResult.success(list);
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult findDistrictTreeByCondition(CommonParameter commonParameter) {
        try {
            List<DistrictEntity> districtEntityList = districtDao.findDistrictListByCondition(commonParameter);
            if (districtEntityList == null || districtEntityList.size() == 0) {
                return ResponseResult.DATA_NOT_AVAILABLE;
            }
            List<DistrictNestingEntity> list = CommonUtils.getDistrictTree(districtEntityList);
            return ResponseResult.success(list);
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult findProvinceAndCityTree() {
        try {
            List<DistrictEntity> districtEntityList = districtDao.findProvinceAndCityList();
            if (districtEntityList == null || districtEntityList.size() == 0) {
                return ResponseResult.DATA_NOT_AVAILABLE;
            }
            List<DistrictNestingEntity> list = CommonUtils.getDistrictTree(districtEntityList);
            return ResponseResult.success(list);
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }
}
